// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.iso;

import java.util.ArrayList;
import java.util.List;
import se.krka.kahlua.vm.KahluaTable;
import zombie.characters.IsoPlayer;
import zombie.core.SpriteRenderer;
import zombie.core.textures.Texture;
import zombie.debug.LineDrawer;
import zombie.network.GameServer;
import zombie.util.Type;

public final class IsoMarkers {
    public static final IsoMarkers instance = new IsoMarkers();
    private static int NextIsoMarkerID = 0;
    private final List<IsoMarkers.IsoMarker> markers = new ArrayList();
    private final List<IsoMarkers.CircleIsoMarker> circlemarkers = new ArrayList();
    private static int NextCircleIsoMarkerID = 0;

    private IsoMarkers() {
    }

    public void init() {
    }

    public void reset() {
        this.markers.clear();
        this.circlemarkers.clear();
    }

    public void update() {
        if (!GameServer.bServer) {
            this.updateIsoMarkers();
            this.updateCircleIsoMarkers();
        }
    }

    private void updateIsoMarkers() {
        if (IsoCamera.frameState.playerIndex == 0) {
            if (this.markers.size() != 0) {
                for (int int0 = this.markers.size() - 1; int0 >= 0; int0--) {
                    if (((IsoMarkers.IsoMarker)this.markers.get(int0)).isRemoved()) {
                        if (((IsoMarkers.IsoMarker)this.markers.get(int0)).hasTempSquareObject()) {
                            ((IsoMarkers.IsoMarker)this.markers.get(int0)).removeTempSquareObjects();
                        }

                        this.markers.remove(int0);
                    }
                }

                for (int int1 = 0; int1 < this.markers.size(); int1++) {
                    IsoMarkers.IsoMarker isoMarker = (IsoMarkers.IsoMarker)this.markers.get(int1);
                    if (isoMarker.alphaInc) {
                        isoMarker.alpha = isoMarker.alpha + zombie.GameTime.getInstance().getMultiplier() * isoMarker.fadeSpeed;
                        if (isoMarker.alpha > isoMarker.alphaMax) {
                            isoMarker.alphaInc = false;
                            isoMarker.alpha = isoMarker.alphaMax;
                        }
                    } else {
                        isoMarker.alpha = isoMarker.alpha - zombie.GameTime.getInstance().getMultiplier() * isoMarker.fadeSpeed;
                        if (isoMarker.alpha < isoMarker.alphaMin) {
                            isoMarker.alphaInc = true;
                            isoMarker.alpha = 0.3F;
                        }
                    }
                }
            }
        }
    }

    public boolean removeIsoMarker(IsoMarkers.IsoMarker marker) {
        return this.removeIsoMarker(marker.getID());
    }

    public boolean removeIsoMarker(int id) {
        for (int _int = this.markers.size() - 1; _int >= 0; _int--) {
            if (((IsoMarkers.IsoMarker)this.markers.get(_int)).getID() == id) {
                ((IsoMarkers.IsoMarker)this.markers.get(_int)).remove();
                this.markers.remove(_int);
                return true;
            }
        }

        return false;
    }

    public IsoMarkers.IsoMarker getIsoMarker(int id) {
        for (int _int = 0; _int < this.markers.size(); _int++) {
            if (((IsoMarkers.IsoMarker)this.markers.get(_int)).getID() == id) {
                return (IsoMarkers.IsoMarker)this.markers.get(_int);
            }
        }

        return null;
    }

    public IsoMarkers.IsoMarker addIsoMarker(String spriteName, IsoGridSquare gs, float r, float g, float b, boolean doAlpha, boolean doIsoObject) {
        if (GameServer.bServer) {
            return null;
        } else {
            IsoMarkers.IsoMarker isoMarker = new IsoMarkers.IsoMarker();
            isoMarker.setSquare(gs);
            isoMarker.init(spriteName, gs.x, gs.y, gs.z, gs, doIsoObject);
            isoMarker.setR(r);
            isoMarker.setG(g);
            isoMarker.setB(b);
            isoMarker.setA(1.0F);
            isoMarker.setDoAlpha(doAlpha);
            isoMarker.setFadeSpeed(0.006F);
            isoMarker.setAlpha(1.0F);
            isoMarker.setAlphaMin(0.3F);
            isoMarker.setAlphaMax(1.0F);
            this.markers.add(isoMarker);
            return isoMarker;
        }
    }

    public IsoMarkers.IsoMarker addIsoMarker(
        KahluaTable kahluaTable0,
        KahluaTable kahluaTable1,
        IsoGridSquare isoGridSquare,
        float float0,
        float float1,
        float float2,
        boolean boolean0,
        boolean boolean1
    ) {
        return this.addIsoMarker(kahluaTable0, kahluaTable1, isoGridSquare, float0, float1, float2, boolean0, boolean1, 0.006F, 0.3F, 1.0F);
    }

    public IsoMarkers.IsoMarker addIsoMarker(
        KahluaTable kahluaTable0,
        KahluaTable kahluaTable1,
        IsoGridSquare isoGridSquare,
        float float0,
        float float1,
        float float2,
        boolean boolean1,
        boolean boolean0,
        float float3,
        float float4,
        float float5
    ) {
        if (GameServer.bServer) {
            return null;
        } else {
            IsoMarkers.IsoMarker isoMarker = new IsoMarkers.IsoMarker();
            isoMarker.init(kahluaTable0, kahluaTable1, isoGridSquare.x, isoGridSquare.y, isoGridSquare.z, isoGridSquare, boolean0);
            isoMarker.setSquare(isoGridSquare);
            isoMarker.setR(float0);
            isoMarker.setG(float1);
            isoMarker.setB(float2);
            isoMarker.setA(1.0F);
            isoMarker.setDoAlpha(boolean1);
            isoMarker.setFadeSpeed(float3);
            isoMarker.setAlpha(0.0F);
            isoMarker.setAlphaMin(float4);
            isoMarker.setAlphaMax(float5);
            this.markers.add(isoMarker);
            return isoMarker;
        }
    }

    public void renderIsoMarkers(IsoCell.PerPlayerRender perPlayerRender, int zLayer, int playerIndex) {
        if (!GameServer.bServer && this.markers.size() != 0) {
            IsoPlayer isoPlayer = IsoPlayer.players[playerIndex];
            if (isoPlayer != null) {
                for (int int0 = 0; int0 < this.markers.size(); int0++) {
                    IsoMarkers.IsoMarker isoMarker = (IsoMarkers.IsoMarker)this.markers.get(int0);
                    if (isoMarker.z == (float)zLayer && isoMarker.z == isoPlayer.getZ() && isoMarker.active) {
                        for (int int1 = 0; int1 < isoMarker.textures.size(); int1++) {
                            Texture texture = (Texture)isoMarker.textures.get(int1);
                            float float0 = IsoUtils.XToScreen(isoMarker.x, isoMarker.y, isoMarker.z, 0)
                                - IsoCamera.cameras[playerIndex].getOffX()
                                - (float)texture.getWidth() / 2.0F;
                            float float1 = IsoUtils.YToScreen(isoMarker.x, isoMarker.y, isoMarker.z, 0)
                                - IsoCamera.cameras[playerIndex].getOffY()
                                - (float)texture.getHeight();
                            SpriteRenderer.instance
                                .render(
                                    texture,
                                    float0,
                                    float1,
                                    (float)texture.getWidth(),
                                    (float)texture.getHeight(),
                                    isoMarker.r,
                                    isoMarker.g,
                                    isoMarker.b,
                                    isoMarker.alpha,
                                    null
                                );
                        }
                    }
                }
            }
        }
    }

    public void renderIsoMarkersDeferred(IsoCell.PerPlayerRender perPlayerRender, int zLayer, int playerIndex) {
        if (!GameServer.bServer && this.markers.size() != 0) {
            IsoPlayer isoPlayer = IsoPlayer.players[playerIndex];
            if (isoPlayer != null) {
                for (int int0 = 0; int0 < this.markers.size(); int0++) {
                    IsoMarkers.IsoMarker isoMarker = (IsoMarkers.IsoMarker)this.markers.get(int0);
                    if (isoMarker.z == (float)zLayer && isoMarker.z == isoPlayer.getZ() && isoMarker.active) {
                        for (int int1 = 0; int1 < isoMarker.overlayTextures.size(); int1++) {
                            Texture texture = (Texture)isoMarker.overlayTextures.get(int1);
                            float float0 = IsoUtils.XToScreen(isoMarker.x, isoMarker.y, isoMarker.z, 0)
                                - IsoCamera.cameras[playerIndex].getOffX()
                                - (float)texture.getWidth() / 2.0F;
                            float float1 = IsoUtils.YToScreen(isoMarker.x, isoMarker.y, isoMarker.z, 0)
                                - IsoCamera.cameras[playerIndex].getOffY()
                                - (float)texture.getHeight();
                            SpriteRenderer.instance
                                .render(
                                    texture,
                                    float0,
                                    float1,
                                    (float)texture.getWidth(),
                                    (float)texture.getHeight(),
                                    isoMarker.r,
                                    isoMarker.g,
                                    isoMarker.b,
                                    isoMarker.alpha,
                                    null
                                );
                        }
                    }
                }
            }
        }
    }

    public void renderIsoMarkersOnSquare(IsoCell.PerPlayerRender perPlayerRender, int zLayer, int playerIndex) {
        if (!GameServer.bServer && this.markers.size() != 0) {
            IsoPlayer isoPlayer = IsoPlayer.players[playerIndex];
            if (isoPlayer != null) {
                for (int int0 = 0; int0 < this.markers.size(); int0++) {
                    IsoMarkers.IsoMarker isoMarker = (IsoMarkers.IsoMarker)this.markers.get(int0);
                    if (isoMarker.z == (float)zLayer && isoMarker.z == isoPlayer.getZ() && isoMarker.active) {
                        for (int int1 = 0; int1 < isoMarker.overlayTextures.size(); int1++) {
                            Texture texture = (Texture)isoMarker.overlayTextures.get(int1);
                            float float0 = IsoUtils.XToScreen(isoMarker.x, isoMarker.y, isoMarker.z, 0)
                                - IsoCamera.cameras[playerIndex].getOffX()
                                - (float)texture.getWidth() / 2.0F;
                            float float1 = IsoUtils.YToScreen(isoMarker.x, isoMarker.y, isoMarker.z, 0)
                                - IsoCamera.cameras[playerIndex].getOffY()
                                - (float)texture.getHeight();
                            SpriteRenderer.instance
                                .render(
                                    texture,
                                    float0,
                                    float1,
                                    (float)texture.getWidth(),
                                    (float)texture.getHeight(),
                                    isoMarker.r,
                                    isoMarker.g,
                                    isoMarker.b,
                                    isoMarker.alpha,
                                    null
                                );
                        }
                    }
                }
            }
        }
    }

    private void updateCircleIsoMarkers() {
        if (IsoCamera.frameState.playerIndex == 0) {
            if (this.circlemarkers.size() != 0) {
                for (int int0 = this.circlemarkers.size() - 1; int0 >= 0; int0--) {
                    if (((IsoMarkers.CircleIsoMarker)this.circlemarkers.get(int0)).isRemoved()) {
                        this.circlemarkers.remove(int0);
                    }
                }

                for (int int1 = 0; int1 < this.circlemarkers.size(); int1++) {
                    IsoMarkers.CircleIsoMarker circleIsoMarker = (IsoMarkers.CircleIsoMarker)this.circlemarkers.get(int1);
                    if (circleIsoMarker.alphaInc) {
                        circleIsoMarker.alpha = circleIsoMarker.alpha + zombie.GameTime.getInstance().getMultiplier() * circleIsoMarker.fadeSpeed;
                        if (circleIsoMarker.alpha > circleIsoMarker.alphaMax) {
                            circleIsoMarker.alphaInc = false;
                            circleIsoMarker.alpha = circleIsoMarker.alphaMax;
                        }
                    } else {
                        circleIsoMarker.alpha = circleIsoMarker.alpha - zombie.GameTime.getInstance().getMultiplier() * circleIsoMarker.fadeSpeed;
                        if (circleIsoMarker.alpha < circleIsoMarker.alphaMin) {
                            circleIsoMarker.alphaInc = true;
                            circleIsoMarker.alpha = 0.3F;
                        }
                    }
                }
            }
        }
    }

    public boolean removeCircleIsoMarker(IsoMarkers.CircleIsoMarker marker) {
        return this.removeCircleIsoMarker(marker.getID());
    }

    public boolean removeCircleIsoMarker(int id) {
        for (int _int = this.circlemarkers.size() - 1; _int >= 0; _int--) {
            if (((IsoMarkers.CircleIsoMarker)this.circlemarkers.get(_int)).getID() == id) {
                ((IsoMarkers.CircleIsoMarker)this.circlemarkers.get(_int)).remove();
                this.circlemarkers.remove(_int);
                return true;
            }
        }

        return false;
    }

    public IsoMarkers.CircleIsoMarker getCircleIsoMarker(int id) {
        for (int _int = 0; _int < this.circlemarkers.size(); _int++) {
            if (((IsoMarkers.CircleIsoMarker)this.circlemarkers.get(_int)).getID() == id) {
                return (IsoMarkers.CircleIsoMarker)this.circlemarkers.get(_int);
            }
        }

        return null;
    }

    public IsoMarkers.CircleIsoMarker addCircleIsoMarker(IsoGridSquare gs, float r, float g, float b, float a) {
        if (GameServer.bServer) {
            return null;
        } else {
            IsoMarkers.CircleIsoMarker circleIsoMarker = new IsoMarkers.CircleIsoMarker();
            circleIsoMarker.init(gs.x, gs.y, gs.z, gs);
            circleIsoMarker.setSquare(gs);
            circleIsoMarker.setR(r);
            circleIsoMarker.setG(g);
            circleIsoMarker.setB(b);
            circleIsoMarker.setA(a);
            circleIsoMarker.setDoAlpha(false);
            circleIsoMarker.setFadeSpeed(0.006F);
            circleIsoMarker.setAlpha(1.0F);
            circleIsoMarker.setAlphaMin(1.0F);
            circleIsoMarker.setAlphaMax(1.0F);
            this.circlemarkers.add(circleIsoMarker);
            return circleIsoMarker;
        }
    }

    public void renderCircleIsoMarkers(IsoCell.PerPlayerRender perPlayerRender, int zLayer, int playerIndex) {
        if (!GameServer.bServer && this.circlemarkers.size() != 0) {
            IsoPlayer isoPlayer = IsoPlayer.players[playerIndex];
            if (isoPlayer != null) {
                for (int _int = 0; _int < this.circlemarkers.size(); _int++) {
                    IsoMarkers.CircleIsoMarker circleIsoMarker = (IsoMarkers.CircleIsoMarker)this.circlemarkers.get(_int);
                    if (circleIsoMarker.z == (float)zLayer && circleIsoMarker.z == isoPlayer.getZ() && circleIsoMarker.active) {
                        LineDrawer.DrawIsoCircle(
                            circleIsoMarker.x,
                            circleIsoMarker.y,
                            circleIsoMarker.z,
                            circleIsoMarker.size,
                            32,
                            circleIsoMarker.r,
                            circleIsoMarker.g,
                            circleIsoMarker.b,
                            circleIsoMarker.a
                        );
                    }
                }
            }
        }
    }

    public void render() {
        this.update();
    }

    public static final class CircleIsoMarker {
        private int ID;
        private IsoGridSquare square;
        private float x;
        private float y;
        private float z;
        private float r;
        private float g;
        private float b;
        private float a;
        private float size;
        private boolean doAlpha;
        private float fadeSpeed = 0.006F;
        private float alpha = 0.0F;
        private float alphaMax = 1.0F;
        private float alphaMin = 0.3F;
        private boolean alphaInc = true;
        private boolean active = true;
        private boolean isRemoved = false;

        public CircleIsoMarker() {
            this.ID = IsoMarkers.NextCircleIsoMarkerID++;
        }

        public int getID() {
            return this.ID;
        }

        public void remove() {
            this.isRemoved = true;
        }

        public boolean isRemoved() {
            return this.isRemoved;
        }

        public void init(int _x, int _y, int _z, IsoGridSquare gs) {
            this.square = gs;
        }

        public float getX() {
            return this.x;
        }

        public float getY() {
            return this.y;
        }

        public float getZ() {
            return this.z;
        }

        public float getR() {
            return this.r;
        }

        public float getG() {
            return this.g;
        }

        public float getB() {
            return this.b;
        }

        public float getA() {
            return this.a;
        }

        public void setR(float _r) {
            this.r = _r;
        }

        public void setG(float _g) {
            this.g = _g;
        }

        public void setB(float _b) {
            this.b = _b;
        }

        public void setA(float _a) {
            this.a = _a;
        }

        public float getSize() {
            return this.size;
        }

        public void setSize(float _size) {
            this.size = _size;
        }

        public float getAlpha() {
            return this.alpha;
        }

        public void setAlpha(float _alpha) {
            this.alpha = _alpha;
        }

        public float getAlphaMax() {
            return this.alphaMax;
        }

        public void setAlphaMax(float _alphaMax) {
            this.alphaMax = _alphaMax;
        }

        public float getAlphaMin() {
            return this.alphaMin;
        }

        public void setAlphaMin(float _alphaMin) {
            this.alphaMin = _alphaMin;
        }

        public boolean isDoAlpha() {
            return this.doAlpha;
        }

        public void setDoAlpha(boolean _doAlpha) {
            this.doAlpha = _doAlpha;
        }

        public float getFadeSpeed() {
            return this.fadeSpeed;
        }

        public void setFadeSpeed(float _fadeSpeed) {
            this.fadeSpeed = _fadeSpeed;
        }

        public IsoGridSquare getSquare() {
            return this.square;
        }

        public void setSquare(IsoGridSquare _square) {
            this.square = _square;
        }

        public void setPos(int _x, int _y, int _z) {
            this.x = (float)_x;
            this.y = (float)_y;
            this.z = (float)_z;
        }

        public boolean isActive() {
            return this.active;
        }

        public void setActive(boolean _active) {
            this.active = _active;
        }
    }

    public static final class IsoMarker {
        private int ID;
        private ArrayList<Texture> textures = new ArrayList();
        private ArrayList<Texture> overlayTextures = new ArrayList();
        private ArrayList<IsoObject> tempObjects = new ArrayList();
        private IsoGridSquare square;
        private float x;
        private float y;
        private float z;
        private float r;
        private float g;
        private float b;
        private float a;
        private boolean doAlpha;
        private float fadeSpeed = 0.006F;
        private float alpha = 0.0F;
        private float alphaMax = 1.0F;
        private float alphaMin = 0.3F;
        private boolean alphaInc = true;
        private boolean active = true;
        private boolean isRemoved = false;

        public IsoMarker() {
            this.ID = IsoMarkers.NextIsoMarkerID++;
        }

        public int getID() {
            return this.ID;
        }

        public void remove() {
            this.isRemoved = true;
        }

        public boolean isRemoved() {
            return this.isRemoved;
        }

        public void init(KahluaTable kahluaTable0, KahluaTable kahluaTable1, int int2, int int3, int int4, IsoGridSquare isoGridSquare) {
            this.square = isoGridSquare;
            if (kahluaTable0 != null) {
                int int0 = kahluaTable0.len();

                for (int int1 = 1; int1 <= int0; int1++) {
                    String string0 = Type.tryCastTo(kahluaTable0.rawget(int1), String.class);
                    Texture texture0 = Texture.trygetTexture(string0);
                    if (texture0 != null) {
                        this.textures.add(texture0);
                        this.setPos(int2, int3, int4);
                    }
                }
            }

            if (kahluaTable1 != null) {
                int int5 = kahluaTable1.len();

                for (int int6 = 1; int6 <= int5; int6++) {
                    String string1 = Type.tryCastTo(kahluaTable1.rawget(int6), String.class);
                    Texture texture1 = Texture.trygetTexture(string1);
                    if (texture1 != null) {
                        this.overlayTextures.add(texture1);
                        this.setPos(int2, int3, int4);
                    }
                }
            }
        }

        public void init(KahluaTable kahluaTable0, KahluaTable kahluaTable1, int int2, int int3, int int4, IsoGridSquare isoGridSquare, boolean _boolean) {
            this.square = isoGridSquare;
            if (_boolean) {
                if (kahluaTable0 != null) {
                    int int0 = kahluaTable0.len();

                    for (int int1 = 1; int1 <= int0; int1++) {
                        String string = Type.tryCastTo(kahluaTable0.rawget(int1), String.class);
                        Texture texture = Texture.trygetTexture(string);
                        if (texture != null) {
                            IsoObject isoObject = new IsoObject(isoGridSquare.getCell(), isoGridSquare, texture.getName());
                            this.tempObjects.add(isoObject);
                            this.addTempSquareObject(isoObject);
                            this.setPos(int2, int3, int4);
                        }
                    }
                }
            } else {
                this.init(kahluaTable0, kahluaTable1, int2, int3, int4, isoGridSquare);
            }
        }

        public void init(String spriteName, int _x, int _y, int _z, IsoGridSquare gs, boolean doTempIsoObject) {
            this.square = gs;
            if (doTempIsoObject && spriteName != null) {
                IsoObject isoObject = IsoObject.getNew(gs, spriteName, spriteName, false);
                this.tempObjects.add(isoObject);
                this.addTempSquareObject(isoObject);
                this.setPos(_x, _y, _z);
            }
        }

        public boolean hasTempSquareObject() {
            return this.tempObjects.size() > 0;
        }

        public void addTempSquareObject(IsoObject tempObject) {
            this.square.localTemporaryObjects.add(tempObject);
        }

        public void removeTempSquareObjects() {
            this.square.localTemporaryObjects.clear();
        }

        public float getX() {
            return this.x;
        }

        public float getY() {
            return this.y;
        }

        public float getZ() {
            return this.z;
        }

        public float getR() {
            return this.r;
        }

        public float getG() {
            return this.g;
        }

        public float getB() {
            return this.b;
        }

        public float getA() {
            return this.a;
        }

        public void setR(float _r) {
            this.r = _r;
        }

        public void setG(float _g) {
            this.g = _g;
        }

        public void setB(float _b) {
            this.b = _b;
        }

        public void setA(float _a) {
            this.a = _a;
        }

        public float getAlpha() {
            return this.alpha;
        }

        public void setAlpha(float _alpha) {
            this.alpha = _alpha;
        }

        public float getAlphaMax() {
            return this.alphaMax;
        }

        public void setAlphaMax(float _alphaMax) {
            this.alphaMax = _alphaMax;
        }

        public float getAlphaMin() {
            return this.alphaMin;
        }

        public void setAlphaMin(float _alphaMin) {
            this.alphaMin = _alphaMin;
        }

        public boolean isDoAlpha() {
            return this.doAlpha;
        }

        public void setDoAlpha(boolean _doAlpha) {
            this.doAlpha = _doAlpha;
        }

        public float getFadeSpeed() {
            return this.fadeSpeed;
        }

        public void setFadeSpeed(float _fadeSpeed) {
            this.fadeSpeed = _fadeSpeed;
        }

        public IsoGridSquare getSquare() {
            return this.square;
        }

        public void setSquare(IsoGridSquare _square) {
            this.square = _square;
        }

        public void setPos(int _x, int _y, int _z) {
            this.x = (float)_x + 0.5F;
            this.y = (float)_y + 0.5F;
            this.z = (float)_z;
        }

        public boolean isActive() {
            return this.active;
        }

        public void setActive(boolean _active) {
            this.active = _active;
        }
    }
}
